﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using EasyIot.Model;
using EasyIot.Repository;

namespace EasyIot.Service
{
    public class ItemsService : BaseService<ItemsModel>
    {
        public static dynamic GetListByFilter(ItemsModel filter, PageInfo pageInfo)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Items treeSelect数据列表
        /// </summary>
        public static IEnumerable<TreeSelect> GetItemsTreeSelect()
        {
            IEnumerable<ItemsModel> moduleList = BaseRepository<ItemsModel>.GetAll("Id,FullName,ParentId", "ORDER BY SortCode ASC");
            var rootModuleList = moduleList.Where(x => x.ParentId == 0).OrderBy(x => x.SortCode);
            List<TreeSelect> treeSelectList = new List<TreeSelect>();
            foreach (var item in rootModuleList)
            {
                TreeSelect tree = new TreeSelect
                {
                    id = item.Id,
                    name = item.FullName,
                    open = false
                };
                GetItemsChildren(treeSelectList, moduleList, tree, item.Id);
                treeSelectList.Add(tree);
            }
            return treeSelectList;
        }

        /// <summary>
        /// 递归遍历treeSelectList
        /// </summary>
        private static void GetItemsChildren(List<TreeSelect> treeSelectList, IEnumerable<ItemsModel> moduleList, TreeSelect tree, int id)
        {
            var childModuleList = moduleList.Where(x => x.ParentId == id).OrderBy(x => x.SortCode);
            if (childModuleList != null && childModuleList.Count() > 0)
            {
                List<TreeSelect> _children = new List<TreeSelect>();
                foreach (var item in childModuleList)
                {
                    TreeSelect _tree = new TreeSelect
                    {
                        id = item.Id,
                        name = item.FullName,
                        open = false
                    };
                    _children.Add(_tree);
                    tree.children = _children;
                    GetItemsChildren(treeSelectList, moduleList, _tree, item.Id);
                }
            }
        }

        public static bool DeleteContactById(int id)
        {
            string param = "@id0"; int index = 1; string deleteWhere1 = "where Id in (" + param + ","; string deleteWhere2 = "where ItemId in (" + param + ",";

            DynamicParameters dpar1 = new DynamicParameters(); dpar1.Add(param, id); DynamicParameters dpar2 = new DynamicParameters(); dpar2.Add(param, id); 

            string where = "where ParentId=@ParentId";
            IEnumerable<ItemsModel> notes = null;

            while (null == notes || notes.Count() > 0)
            {
                notes = GetByWhere(where, new { ParentId = id });
                foreach (var note in notes)
                {
                    id = note.Id;

                    param = "@id" + index;
                    deleteWhere1 += param + ","; deleteWhere2 += param + ",";

                    dpar1.Add(param, id); dpar2.Add(param, id);

                    index++;
                }
            }

            deleteWhere1 = deleteWhere1.Substring(0, deleteWhere1.Length - 1); deleteWhere1 += ")";

            deleteWhere2 = deleteWhere2.Substring(0, deleteWhere2.Length - 1); deleteWhere2 += ")";

            ItemsDetailService.DeleteContactById(deleteWhere2, dpar2);

            return DeleteByWhere(deleteWhere1, dpar1);
        }

        public static bool IsExistCode(string encode)
        {
            string where = "where EnCode=@EnCode";
            return GetByWhere(where, new { EnCode = encode }).Count() > 0 ? true : false;
        } 
    }
}
